using System;
using System.Collections.Generic;
using System.Linq;

namespace TemplateWeb.Models.Repositories
{
    #region using
    using TemplateWeb.Models;
    using TemplateWeb.Helpers;
    #endregion
    public class DonDatHangRepository
    {
        public List<DonDatHang> SelectAll()
        {
            try
            {
                return DataContext.Instance.DonDatHang.ToList();
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    CommonLogging.Logger.Error(ex.InnerException.Message);
                }
                else
                {
                    CommonLogging.Logger.Error(ex.Message);
                }
            }
            return new List<DonDatHang>();
        }

        public DonDatHang SelectByID(int id)
        {
            try
            {
                return DataContext.Instance.DonDatHang.FirstOrDefault(item => item.ID == id);
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    CommonLogging.Logger.Error(ex.InnerException.Message);
                }
                else
                {
                    CommonLogging.Logger.Error(ex.Message);
                }
            }
            return null;
        }

        public bool Insert(DonDatHang obj)
        {
            try
            {
                DataContext.Instance.DonDatHang.AddObject(obj);
                DataContext.Instance.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    CommonLogging.Logger.Error(ex.InnerException.Message);
                }
                else
                {
                    CommonLogging.Logger.Error(ex.Message);
                }
            }
            return false;// Cannot insert
        }

        public bool Delete(int id)
        {
            try
            {
                var delitem = DataContext.Instance.DonDatHang.FirstOrDefault(item => item.ID == id);
                DataContext.Instance.DonDatHang.DeleteObject(delitem);
                DataContext.Instance.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    CommonLogging.Logger.Error(ex.InnerException.Message);
                }
                else
                {
                    CommonLogging.Logger.Error(ex.Message);
                }
            }
            return false;
        }

        public bool Save()
        {
            try
            {
                DataContext.Instance.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    CommonLogging.Logger.Error(ex.InnerException.Message);
                }
                else
                {
                    CommonLogging.Logger.Error(ex.Message);
                }
            }
            return false;
        }

        public List<DonDatHang> RetrieveByID(int id)
        {
            try
            {
                return (from item in DataContext.Instance.DonDatHang where item.ID == id select item).ToList();
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    CommonLogging.Logger.Error(ex.InnerException.Message);
                }
                else
                {
                    CommonLogging.Logger.Error(ex.Message);
                }
            }
            return new List<DonDatHang>();
        }

        public List<DonDatHang> SelectByIDUsers(Guid iduser)
        {
            try
            {
                var lstItem = (from item in DataContext.Instance.DonDatHang where item.IDUser == iduser select item).OrderByDescending(n => n.NgayDatHang).ToList();
                return lstItem;
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    CommonLogging.Logger.Error(ex.InnerException.Message);
                }
                else
                {
                    CommonLogging.Logger.Error(ex.Message);
                }
            }
            return new List<DonDatHang>();
        }

        public List<DonDatHang> Search(string idkhachhang, string daxacnhan, string dagiaohang, string dahuy, string khuvuc)
        {
            var lst = new List<DonDatHang>();
            try
            {
                foreach (var item in DataContext.Instance.DonDatHang)
                {
                    var daxacnhan_value = daxacnhan.Equals(string.Empty) ? item.DaXacNhan.Value : bool.Parse(daxacnhan);
                    var dagiaohang_value = dagiaohang.Equals(string.Empty) ? item.DaGiaoHang.Value : bool.Parse(dagiaohang);
                    var dahuy_value = dahuy.Equals(string.Empty) ? item.HuyDonHang.Value : bool.Parse(dahuy);

                    if (item.NguoiDung.aspnet_Users.UserName.ToString().Equals(idkhachhang) &&
                        item.DaXacNhan.Value == daxacnhan_value &&
                        item.DaGiaoHang.Value == dagiaohang_value &&
                        item.HuyDonHang.Value == dahuy_value &&
                        item.DiaChiNguoiNhan.ToUpper().Contains(khuvuc.Equals(string.Empty) ? item.DiaChiNguoiNhan.ToUpper() : khuvuc.ToUpper()))
                    {
                        lst.Add(item);
                    }
                }
                lst = lst.OrderBy(n => n.NgayDatHang).ToList();
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                    CommonLogging.Logger.Error("DonDatHang - Search: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
                else
                    CommonLogging.Logger.Error("DonDatHang - Search: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
            }
            return lst;
        }
    }
}